<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<table class="gio-table-light gio-form-listeners__table" [formGroup]="mainForm">
  <thead>
    <tr>
      <th>
        <div class="gio-form-listeners__table__title">Virtual host</div>
        <div class="gio-form-listeners__table__subtitle">Host that must be set in the HTTP request to access your entrypoint.</div>
      </th>
      <th>
        <div class="gio-form-listeners__table__title">
          Context-path
          <mat-icon
            class="gio-form-listeners__table__title__icon"
            matTooltip="The URL location of your API. So if your URL is [https://api.mycompany.com/myAPI] then [/myAPI] is the context path."
            svgIcon="gio:info"
          ></mat-icon>
        </div>
        <div class="gio-form-listeners__table__subtitle">
          Must start with a '/' and can contain an uppercase letter, number, dash or underscore.
        </div>
      </th>
      <th class="gio-form-listeners__table__row__th">
        <div class="gio-form-listeners__table__title">Override access</div>
        <div class="gio-form-listeners__table__subtitle">Enable override on the access URL of your Portal using virtual host.</div>
      </th>
      <th class="gio-form-listeners__table__row__th"></th>
    </tr>
  </thead>
  <tbody formArrayName="listeners">
    <tr
      *ngFor="let control of listenerFormArray.controls; index as controlIndex"
      [formGroupName]="controlIndex"
      class="gio-form-listeners__table__row"
    >
      <td class="gio-form-listeners__table__row__td">
        <mat-form-field class="gio-form-listeners__table__field">
          <input #hostInput type="text" matInput formControlName="_hostSubDomain" placeholder="Host *" [matAutocomplete]="hostAuto" />

          <!-- Config Autocomplete to only select domain with input value -->
          <mat-autocomplete #hostAuto="matAutocomplete">
            <mat-option
              *ngFor="let domain of domainRestrictions"
              [value]="hostInput.value"
              (click)="control.get('_hostDomain').setValue(domain)"
            >
              {{ hostInput.value ? hostInput.value + '.' + domain : domain }}
            </mat-option>
          </mat-autocomplete>

          <span matTextSuffix class="gio-form-listeners__table__field__host"
            ><em>{{ control.get('_hostDomain').value }}</em></span
          >
          <span class="mat-error" *ngIf="control.hasError('host')"> {{ control.getError('host') }}</span>
        </mat-form-field>
      </td>
      <td class="gio-form-listeners__table__row__td">
        <mat-form-field class="gio-form-listeners__table__field">
          <input type="text" matInput formControlName="path" placeholder="Context-path *" />
        </mat-form-field>
        <mat-error *ngIf="control.get('path').hasError('contextPath')"> {{ control.get('path').getError('contextPath') }}</mat-error>
        <mat-error *ngIf="control.hasError('listeners')"> {{ control.getError('listeners') }}</mat-error>
      </td>
      <td class="gio-form-listeners__table__row__td">
        <gio-form-slide-toggle class="gio-form-listeners__table__field">
          <mat-slide-toggle gioFormSlideToggle aria-label="Enable override access" formControlName="overrideAccess"></mat-slide-toggle>
          <gio-form-label>Enable</gio-form-label>
        </gio-form-slide-toggle>
      </td>
      <td class="gio-form-listeners__table__row__td" *ngIf="!isDisabled">
        <button
          mat-icon-button
          type="button"
          aria-label="Delete"
          [class.gio-form-listeners__table__row__td__button__hide]="listenerFormArray.controls.length <= 1"
          (click)="onDelete(controlIndex)"
          class="gio-form-listeners__table__row__td__button"
        >
          <mat-icon svgIcon="gio:cancel"></mat-icon>
        </button>
      </td>
    </tr>
  </tbody>
</table>
<mat-error *ngIf="listenerFormArray.hasError('contextPath')">{{ listenerFormArray.getError('contextPath') }}</mat-error>

<button *ngIf="!isDisabled" class="gio-form-listeners__add-button" type="button" mat-stroked-button (click)="addEmptyListener()">
  Add context-path
</button>
